package com.example.shop.user.controller;

import com.example.shop.common.api.ResponseResult;
import com.example.shop.common.api.ServiceReturn;
import com.example.shop.common.api.ServiceStateCode;
import com.example.shop.common.enums.SessionAttributeKey;
import com.example.shop.pojo.User;
import com.example.shop.user.dto.AddLinkmanParam;
import com.example.shop.user.vo.LinkmanInfo;
import com.example.shop.user.dto.ModifyLinkmanParam;
import com.example.shop.user.service.LinkmanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/user/linkman")
public class LinkmanController {
    @Autowired
    LinkmanService linkmanService;

    @PostMapping("/add")
    public ResponseResult<LinkmanInfo> addLinkman(HttpSession session, @Validated AddLinkmanParam addInfo, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return ResponseResult.failure(ServiceStateCode.PARAMETER_NONSTANDARD);
        }
        User user = (User) session.getAttribute(SessionAttributeKey.NOW_LOGIN_USER.getKey());
        ServiceReturn<LinkmanInfo> result = linkmanService.addLinkman(user,addInfo);
        return ResponseResult.auto(result.getStateCode(),result.getReturnData());
    }

    @GetMapping(value = "/delete/{linkmanId}",consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseResult<Boolean> deleteLinkman(HttpSession session,@PathVariable("linkmanId") long linkmanId){
        User user = (User) session.getAttribute(SessionAttributeKey.NOW_LOGIN_USER.getKey());
        ServiceReturn<Boolean> result = linkmanService.deleteLinkman(user,linkmanId);
        return ResponseResult.auto(result.getStateCode(),result.getReturnData());
    }

    @GetMapping(value = "/get/{linkmanId}",consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseResult<LinkmanInfo> getLinkman(HttpSession session,@PathVariable("linkmanId") long linkmanId){
        User user = (User) session.getAttribute(SessionAttributeKey.NOW_LOGIN_USER.getKey());
        ServiceReturn<LinkmanInfo> result = linkmanService.getLinkman(user,linkmanId);
        return ResponseResult.auto(result.getStateCode(),result.getReturnData());
    }

    @GetMapping(value = "/get",consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseResult<List<LinkmanInfo>>  getAddressBook(HttpSession session){
        User user = (User) session.getAttribute(SessionAttributeKey.NOW_LOGIN_USER.getKey());
        ServiceReturn<List<LinkmanInfo>> result = linkmanService.getAddressBook(user);
        return ResponseResult.auto(result.getStateCode(),result.getReturnData());
    }

    @PostMapping("/modify")
    public ResponseResult<LinkmanInfo> modifyLinkmanInfo(HttpSession session, ModifyLinkmanParam modifyInfo){
        if(modifyInfo == null|| modifyInfo.getLinkmanId() == null){
            return ResponseResult.failure(ServiceStateCode.PARAMETER_NONSTANDARD);
        }
        User user = (User) session.getAttribute(SessionAttributeKey.NOW_LOGIN_USER.getKey());
        ServiceReturn<LinkmanInfo> result = linkmanService.modifyLinkmanInfo(user,modifyInfo);
        return ResponseResult.auto(result.getStateCode(),result.getReturnData());
    }


}
